home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cgazv5n5.arc / LIST9.C < prev    next >
C/C++ Source or Header  |  1991-09-23  |  4KB  |  148 lines

  1. /*---------------------------------------- Listing 9 ------
  2.  * Intelligent use of goto's in error handling. 
  3.  * See Listing 1 for copyright terms.
  4.  *-------------------------------------------------------*/
  5.  
  6. /*  Read two adjacent 5-byte items each from two different 
  7.     files, and compare them. What could be easier?       */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <memory.h>
  12.  
  13.   /*---------------------------------------------------------
  14.    * NOTE: We are passing file handles to the functions and 
  15.    * assume them to be valid. Otherwise, even more exception 
  16.    * handling would be required!
  17.    *-------------------------------------------------------*/
  18.  
  19. void IsThisStructured ( FILE *File1, FILE *File2 );
  20. void IsThisNot ( FILE *File1, FILE *File2 );
  21. void IsThisNotToo ( FILE *File1, FILE *File2 );
  22.  
  23. void IsThisStructured ( FILE *File1, FILE *File2 )
  24. {
  25.     char *Buf1, *Buf2;
  26.     int NumRead;
  27.  
  28.     Buf1 = malloc ( 2 * 5 );
  29.     if ( Buf1 != NULL ) 
  30.     {
  31.         Buf2 = malloc ( 2 * 5 );
  32.         if ( Buf2 != NULL ) 
  33.         {
  34.             NumRead = fread ( Buf1, 5, 2, File1 );
  35.             if ( NumRead == 2 ) 
  36.             {
  37.                 NumRead = fread ( Buf2, 5, 2, File2 );
  38.                 if (NumRead == 2) 
  39.                 {
  40.                     if ( memcmp ( Buf1, Buf2, 2 * 5 ))
  41.                         printf ( "Items do not match!\n" );
  42.                     else
  43.                         printf ( "Items match\n" );
  44.                 }
  45.                 else 
  46.                 {
  47.                     free ( Buf1 );
  48.                     free ( Buf2 );
  49.                     printf ( "I/O read error\n" );
  50.                 }
  51.             }
  52.             else 
  53.             {
  54.                 free ( Buf1 );
  55.                 free ( Buf2 );
  56.                 printf ( "I/O read error\n" );
  57.             }
  58.         }
  59.         else 
  60.         {
  61.             printf ( "Memory Allocation error\n" );
  62.             free ( Buf1 );
  63.         }
  64.     }
  65.     else
  66.         printf ( "Memory Allocation error\n" );
  67. }
  68.  
  69.  
  70. void IsThisNot ( FILE *File1, FILE *File2 )
  71. {
  72.     char *Buf1, *Buf2;
  73.     int NumRead;
  74.  
  75.     Buf1 = malloc ( 2 * 5 );
  76.     if ( Buf1 == NULL )
  77.         goto AllocFailed1;
  78.  
  79.     Buf2 = malloc ( 2 * 5 );
  80.     if ( Buf2 == NULL )
  81.         goto AllocFailed2;
  82.  
  83.     NumRead = fread ( Buf1, 5, 2, File1 );
  84.     if ( NumRead != 2 )
  85.         goto IoFailed;
  86.  
  87.     NumRead = fread ( Buf2, 5, 2, File2 );
  88.     if ( NumRead != 2 )
  89.         goto IoFailed;
  90.  
  91.     if ( memcmp ( Buf1, Buf2, 2 * 5 ))
  92.         printf ( "Items do not match!\n" );
  93.     else
  94.         printf ( "Items match\n" );
  95.  
  96.     return;     /* NEVER forget this, or you'll fall through ! */
  97.  
  98. AllocFailed2:
  99.     free ( Buf1 );
  100. AllocFailed1:
  101.     printf ( "Memory Allocation error\n" );
  102.     return;     /* no fall through */
  103.  
  104. IoFailed:
  105.     free ( Buf1 );
  106.     free ( Buf2 );
  107.     printf( "I/O read error\n" );
  108. }
  109.  
  110.  
  111. #define ExcCheck(ExcCondition, Label) \
  112.     if (ExcCondition) goto Label
  113.  
  114. void IsThisNotToo ( FILE *File1, FILE *File2 )
  115. {
  116.     char *Buf1, *Buf2;
  117.     int NumRead;
  118.  
  119.     Buf1 = malloc ( 2 * 5 );
  120.     ExcCheck ( Buf1 == NULL, AllocFailed1 );
  121.  
  122.     Buf2 = malloc ( 2 * 5 );
  123.     ExcCheck ( Buf2 == NULL, AllocFailed2 );
  124.  
  125.     NumRead = fread ( Buf1, 5, 2, File1 );
  126.     ExcCheck ( NumRead != 2, IoFailed );
  127.  
  128.     NumRead = fread ( Buf2, 5, 2, File2 );
  129.     ExcCheck ( NumRead != 2, IoFailed );
  130.  
  131.     if ( memcmp ( Buf1, Buf2, 2 * 5 ))
  132.         printf ( "Items do not compare!\n" );
  133.     else
  134.         printf( "Items compare\n" );
  135.  
  136.     return;     /* NEVER forget this, or you'll fall through */
  137.  
  138. AllocFailed2:
  139.     free ( Buf1 );
  140. AllocFailed1:
  141.     printf ( "Memory Allocation error\n" );
  142.     return;     /* Don't fall through */
  143.  
  144. IoFailed:
  145.     free ( Buf1 );
  146.     free ( Buf2 );
  147.     printf ( "I/O read error\n" );
  148. }